/*
 * Copyright 2009-2010 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.subversion.server.connection;

import org.internna.iwebjtracker.model.DefaultConnectionDetails;
import org.internna.iwebjtracker.server.connection.ServerConnection;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNWCClient;

/**
 * A wrapper for a physical connection provided by SVNKit.
 *
 * @author Jose Noheda
 * @since 1.0
 */
public final class SVNKitWrapperConnection implements ServerConnection {

    private boolean keepalive;
    private int keepAliveTimeout = -1;
    private SVNURL repositoryURL;
    private SVNWCClient wcClient;
    private SVNClientManager manager;
    private DefaultConnectionDetails connectionDetails;

    /**
     * Accessor method.
     */
    public DefaultConnectionDetails getConnectionDetails() {
        return connectionDetails;
    }

    /**
     * Accessor method.
     */
    public void setConnectionDetails(DefaultConnectionDetails connectionDetails) {
        this.connectionDetails = connectionDetails;
    }

    /**
     * Accessor method.
     */
    public SVNClientManager getManager() {
        return manager;
    }

    /**
     * Accessor method.
     */
    public void setManager(SVNClientManager manager) {
        this.manager = manager;
    }

    /**
     * Accessor method.
     */
    public SVNWCClient getWcClient() {
        return wcClient;
    }

    /**
     * Accessor method.
     */
    public void setWcClient(SVNWCClient wcClient) {
        this.wcClient = wcClient;
    }

    @Override public boolean isOpen() {
        return getManager() != null;
    }

    /**
     * Checks if a working copy client exists.
     */
    @Override public boolean isClosable() {
        return wcClient != null;
    }

    /**
     * Accessor method.
     */
    @Override public boolean keepAlive() {
        return isKeepalive();
    }

    /**
     * See getKeepAliveTimeout
     */
    @Override public int keepAliveTimeout() {
        return getKeepAliveTimeout();
    }

    /**
     * Accessor method.
     */
    public int getKeepAliveTimeout() {
        return keepAliveTimeout;
    }

    /**
     * Accessor method.
     */
    public void setKeepAliveTimeout(int keepAliveTimeout) {
        this.keepAliveTimeout = keepAliveTimeout;
    }

    /**
     * Accessor method.
     */
    public boolean isKeepalive() {
        return keepalive;
    }

    /**
     * Accessor method.
     */
    public void setKeepalive(boolean keepalive) {
        this.keepalive = keepalive;
    }

    /**
     * Accessor method.
     */
    public SVNURL getRepositoryURL() {
        return repositoryURL;
    }

    /**
     * Accessor method.
     */
    public void setRepositoryURL(SVNURL repositoryURL) {
        this.repositoryURL = repositoryURL;
    }

    /**
     * Cleans the references to the connection objects that are being holded.
     */
    @Override public void close() {
        setWcClient(null);
        setManager(null);
    }

}
